from django.shortcuts import render
from .models import Medicine_use
import jwt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework_simplejwt.tokens import RefreshToken
from datetime import datetime
from medicine.models import Medicine

from prescription.models import Prescription

import os
from django.utils.timezone import now
from django.conf import settings
# Create your views here.





class Get_All(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        prescription_id = request.GET.get('prescription_id')

        if not prescription_id:
            return Response({"error": "prescription_id is required"}, status=400)

        try:
            medicine_uses = Medicine_use.objects.filter(prescription_id=prescription_id)

        except Medicine_use.DoesNotExist:
            return Response({"message": "Medicine_use not found"}, status=200)

        if medicine_uses:
            medicine_uses_data = []
            for medicine_use in medicine_uses:
                # 在返回数据时将 medicine_name 添加到字典中
                medicine_use_data = {
                    'medicine_id': medicine_use.medicine_id.id,
                    'id': medicine_use.id,
                    'prescription_id': medicine_use.prescription_id.id,
                    'pay_status': medicine_use.pay_status,
                    'take_status': medicine_use.take_status,
                    'medicine_name': medicine_use.medicine_id.name,
                    'description': medicine_use.description,
                    'dosage': medicine_use.dosage,
                    'frequency': medicine_use.frequency,
                    'num': medicine_use.num,

                }
                medicine_uses_data.append(medicine_use_data)

            return Response({'medicine_uses': medicine_uses_data})

        return Response({"message": "medicine_use not found"}, status=200)

class Get_All_Target(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        prescription_ids = request.GET.get('prescription_ids')

        if not prescription_ids:
            return Response({"error": "prescription_ids is required"}, status=400)
        prescription_id_list = prescription_ids.split(',')
        try:
            medicine_uses = Medicine_use.objects.filter(prescription_id__in=prescription_id_list)

        except Medicine_use.DoesNotExist:
            return Response({"message": "Medicine_use not found"}, status=200)

        if medicine_uses:
            medicine_uses_data = []
            for medicine_use in medicine_uses:

                medicine_use_data = {
                    'medicine_id': medicine_use.medicine_id.id,
                    'id': medicine_use.id,
                    'prescription_id': medicine_use.prescription_id.id,
                    'pay_status': medicine_use.pay_status,
                    'take_status': medicine_use.take_status,
                    'medicine_name': medicine_use.medicine_id.name,
                    'description': medicine_use.description,
                    'dosage': medicine_use.dosage,
                    'frequency': medicine_use.frequency,
                    'num': medicine_use.num,
                    'counts': medicine_use.medicine_id.counts,
                    'price': medicine_use.medicine_id.price,

                }
                medicine_uses_data.append(medicine_use_data)

            return Response({'medicine_uses': medicine_uses_data})

        return Response({"error": "medicine_use not found"}, status=404)

class Get_Target(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):
        medicine_use_id = request.GET.get('medicine_use_id')

        if not medicine_use_id:
            return Response({"error": "medicine_use_id is required"}, status=400)

        try:

            medicine_use = Medicine_use.objects.select_related('medicine_id').get(id=medicine_use_id)
        except Medicine_use.DoesNotExist:
            return Response({"message": "medicine_use not found"}, status=200)

        if medicine_use:

            return Response({
                            'medicine_id': medicine_use.medicine_id.id,
                            'id': medicine_use.id,
                            'prescription_id': medicine_use.prescription_id.id,
                            'medicine_name': medicine_use.medicine_id.name,
                            'pay_status': medicine_use.pay_status,
                            'take_status': medicine_use.take_status,
                            'description': medicine_use.description,
                            'dosage': medicine_use.dosage,
                            'frequency': medicine_use.frequency,
                            'num': medicine_use.num,
                             }, status=200)

        return Response({"message": "medicine_use not found"}, status=200)

class Change(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        medicine_use_id = request.data.get('medicine_use_id')


        medicine_use_description= request.data.get('medicine_use_description')
        medicine_use_dosage = request.data.get('medicine_use_dosage')
        medicine_use_frequency = request.data.get('medicine_use_frequency')
        medicine_use_num = request.data.get('medicine_use_num')

        if not medicine_use_id:
            return Response({"error": "medicine_use_id is required"}, status=400)

        try:

            medicine_use = Medicine_use.objects.get(id=medicine_use_id)
        except Medicine_use.DoesNotExist:
            return Response({"message": "medicine_use not found"}, status=200)
        if medicine_use_id and medicine_use_description :

            medicine_use.description = medicine_use_description
            medicine_use.dosage = medicine_use_dosage
            medicine_use.frequency = medicine_use_frequency
            medicine_use.num = medicine_use_num
            medicine_use.save()


            return Response({"message": "Success"}, status=200)
        return Response({"error": "Unsuccess"}, status=500)



class Create(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        prescription_id = request.data.get('prescription_id')
        prescription_instance = Prescription.objects.get(id=prescription_id)
        medicine_id = request.data.get('medicine_id')
        medicine_instance = Medicine.objects.get(id=medicine_id)


        if not prescription_id:
            return Response({"error": "prescription_id is required"}, status=400)
        if not medicine_id:
            return Response({"error": "medicine_id is required"}, status=400)

        if prescription_id and medicine_id:
            Medicine_use.objects.create(
                                 prescription_id=prescription_instance,
                                 medicine_id = medicine_instance
                                 )
            return Response({"message": "创建成功"}, status=200)

        return Response({"error": "Unsuccess"}, status=400)

class Delete(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        medicine_use_id = request.data.get('medicine_use_id')

        if not medicine_use_id:
            return Response({"error": "medicine_use_id is required"}, status=400)

        if medicine_use_id :
            try:
                # 查找并删除对应的 Medicine_use 对象
                medicine_use = Medicine_use.objects.get(id=medicine_use_id)
                medicine_use.delete()
                return Response({"message": "Medicine_use deleted successfully"}, status=200)
            except Medicine_use.DoesNotExist:
                return Response({"error": "Medicine_use not found"}, status=404)

        return Response({"error": "Unsuccess"}, status=404)


class Take_medicine(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        prescription_id = request.data.get('prescription_id')

        if not prescription_id:
            return Response({"error": "prescription_id is required"}, status=400)

        if prescription_id :
            try:
                medicine_uses = Medicine_use.objects.filter(prescription_id=prescription_id)
                for medicine_use in medicine_uses:
                    medicine = Medicine.objects.get(id=medicine_use.medicine_id.id)
                    # medicine.counts -= medicine_use.num
                    medicine_use.take_status = 'take'
                    medicine_use.save()
                    # medicine.save()
                prescription = Prescription.objects.get(id=prescription_id)
                prescription.take_status = 'take'
                prescription.save()
                # medicine = Medicine.objects.get(id=medicine_use.medicine_id.id)
                # medicine.counts -= medicine_use.num
                # medicine_use.take_status = 'take'
                # medicine.save()
                # medicine_use.save()
                return Response({"message": "take medicine successfully"}, status=200)
            except Medicine_use.DoesNotExist:
                return Response({"error": "medicine_use not found"}, status=404)

        return Response({"error": "Unsuccess"}, status=404)


class Refund_medicine(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):

        medicine_use_id = request.data.get('medicine_use_id')

        if not medicine_use_id:
            return Response({"error": "medicine_use_id is required"}, status=400)

        if medicine_use_id :
            try:
                medicine_use = Medicine_use.objects.get(id=medicine_use_id)
                medicine = Medicine.objects.get(id=medicine_use.medicine_id.id)
                medicine.counts += medicine_use.num
                medicine.save()
                medicine_use.take_status = 'refund'
                medicine_use.save()
                # medicine_use.medicine_id.counts += medicine_use.num
                # medicine_use.save()
                # medicine_use.medicine_id.save()
                return Response({"message": "refund medicine successfully"}, status=200)
            except Medicine_use.DoesNotExist:
                return Response({"error": "medicine_use not found"}, status=404)

        return Response({"error": "Unsuccess"}, status=404)